package com.water.app.Controller.Manager;

import com.water.app.*;
import com.water.app.interfaces.Goods.GoodsService;
import com.water.app.interfaces.account.LoginService;
import com.water.app.interfaces.account.userService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/api/manager")
public class managerController {

    private static final Logger log = LoggerFactory.getLogger(managerController.class);
    @Resource
    private userService userService;
    @Resource
    private GoodsService goodsService;

    @Resource
    private LoginService loginService;

//    @GetMapping("/selectAllUser")
//    public Result selectAllUser(){
//        log.info("正在查询所有用户");
//        List<User> userList= userService.selectAllUser();
//        return Result.success(1,"查询成功",userList);
//    }

    @PostMapping("/selectByLike")
    public Result selectAllGoods(@RequestParam(value = "name",required = false)String name,
                                 @RequestParam(value = "type",required = false)Integer type,
                                 @RequestParam(value = "status",required = false)Integer status,
                                 @RequestParam(value = "pageSize",required = false)Integer pageSize,
                                 @RequestParam(value = "page",required = false)Integer page){
        log.info("正在查询商品name:{},type:{},status:{},pageSize:{},page:{}",name,type,status,pageSize,page);
        if(name==null||name.isEmpty()){
            name="";
        }
        if(type==null||type<=0){
            type=null;
        }
        if(status==null||status<0||status>2){
            status=null;
        }
        if(pageSize==null||pageSize<=0||pageSize>=25||pageSize%5!=0){
            pageSize=5;
        }
        if(page==null){
            page=1;
        }
        selectByLike selectByLike= goodsService.selectByLikeManager(name,type,status,pageSize,page);
        return Result.success(selectByLike);
    }
    @GetMapping("/selectUser")
    public Result selectUser(){
        log.info("正在查询用户信息");
        Integer userId = Constant.THREAD_LOCAL.get();
        User user=null;
        if(userId!=null) {
            user = loginService.selectUserById(userId);
        }
        if(user!=null) {
            return Result.success(user);
        }else{
            log.info("查询用户信息失败");
            return Result.error("查询用户信息失败");
        }
    }


    @GetMapping("/selectAllType")
    public Result selectAllType(){
        List<String> list = goodsService.selectByAllType();
        return Result.success(list);
    }

}
